package com.leetcode.hash.demo3;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author Zan
 * @Create 2024/5/22 10:29
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/happy-number/description/  快乐数
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while (n != 1 && !record.contains(n)) {
            record.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }

    private int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n / 10;
        }
        return res;
    }
}